﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XChipTool
{
    public partial class XChip_AwbChart : UserControl
    { 
        /// <summary>
      /// 画板宽度
      /// </summary>
        public int BoardWidth { get; set; }
        /// <summary>
        /// 画板高度
        /// </summary>
        public int BoardHeight { get; set; }
        /// <summary>
        /// 画板背景颜色
        /// </summary>
        public Color BoardColor { get; set; }
        /// <summary>
        /// 画图区域颜色
        /// </summary>
        public Color AreasColor { get; set; }
        /// <summary>
        /// 曲线图颜色
        /// </summary>
        public Color GraphColor { get; set; }
        /// <summary>
        /// 坐标轴颜色
        /// </summary>
        public Color AxisColor { get; set; }
        /// <summary>
        /// 刻度线颜色
        /// </summary>
        public Color ScaleColor { get; set; }
        /// <summary>
        /// x轴等分
        /// </summary>
        public int XScaleCount=9;
        /// <summary>
        /// y轴等分
        /// </summary>
        public int YScaleCount=9;

        /// <summary>
        /// X轴范围（data数据里面的实际范围）
        /// </summary>
        public int xRange;
        /// <summary>
        /// Y轴范围（data数据里面的实际范围）
        /// </summary>
        public int yRange;


        public int HorizontalMargin;
        public int VerticalMargin;

        public XChip_AwbChart()
        {
            InitializeComponent();
        }

        private void XChip_AwbChart_Paint(object sender, PaintEventArgs e)
        {
           
            //1.确定曲线图区域
            int iAreaWidth = BoardWidth - 2 * HorizontalMargin;              //画图区域宽度
            int iAreaHeight = BoardHeight - 2 * VerticalMargin;              //画图区域高度
            Point pAreaStart = new Point(HorizontalMargin, VerticalMargin);  //画图区域起点
            Point pAreaEnd = new Point(BoardWidth - HorizontalMargin, BoardHeight - VerticalMargin);             //画图区域终点
            Point pOrigin = new Point(HorizontalMargin, BoardHeight - VerticalMargin);  //原点
            Rectangle rectArea = new Rectangle(pAreaStart, new Size(iAreaWidth, iAreaHeight));
            SolidBrush sbAreaBG = new SolidBrush(AreasColor);
             e.Graphics.FillRectangle(sbAreaBG, rectArea);

            sbAreaBG.Dispose();

            //2.确定坐标轴
            Pen penAxis = new Pen(AxisColor, 1);
            penAxis.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
             e.Graphics.DrawLine(penAxis, pOrigin, new Point(pAreaStart.X, pAreaStart.Y - VerticalMargin / 2));
             e.Graphics.DrawLine(penAxis, pOrigin, new Point(pAreaEnd.X + HorizontalMargin / 2, pAreaEnd.Y));
            //3.确定刻度线和标签
            Pen penScale = new Pen(ScaleColor, 1);
            int fontSize = 8;
            for (int i = 0; i <= XScaleCount; i++)
            {
                int x = i * (iAreaWidth / XScaleCount) + pAreaStart.X;
                 e.Graphics.DrawLine(penScale, x, pAreaStart.Y, x, pAreaEnd.Y);
                string lbl = (i * (int)(xRange / XScaleCount)).ToString("X2");
                //if (xRange == 1440)   //如果按照一天分钟时间显示
                //    lbl = (i * (xRange / XScaleCount) / 60).ToString();
                if (i != 0)
                {  e.Graphics.DrawString(lbl, new Font("微软雅黑", fontSize, FontStyle.Regular), new SolidBrush(AxisColor), new Point(x - fontSize, pAreaEnd.Y + VerticalMargin / 9)); }
            }
            for (int i = 0; i <= YScaleCount; i++)
            {
                int y = pAreaEnd.Y - (i * (iAreaHeight / YScaleCount));
                 e.Graphics.DrawLine(penScale, pAreaStart.X, y, pAreaEnd.X, y);
                string lbl = (i * (int)(yRange / YScaleCount)).ToString("X2");
                 e.Graphics.DrawString(lbl, new Font("微软雅黑", fontSize, FontStyle.Regular), new SolidBrush(AxisColor), new Point(pAreaStart.X - (fontSize * lbl.Length) - HorizontalMargin / 9, y - fontSize / 2));
            }
           
        }
    }
}
